package pom;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class LoginForm {
        private final WebDriver driver;
       
        private By loginformLocator = By.id("form-login");
    private By usernameLocator = By.id("modlgn_username");
    private By passwordLocator = By.id("modlgn_passwd");
    private By loginButtonLocator = By.name("Submit");
   
    public LoginForm(WebDriver driver) {
        this.driver = driver;
       
        if (!driver.findElement(loginformLocator).isDisplayed()) {
            throw new IllegalStateException("There is no login form");
        }
    }
   
    public LoginForm typeUsername(String username) {
       
        driver.findElement(usernameLocator).sendKeys(username);

        return this;    
    }
   
    public LoginForm typePassword(String password) {
       
        driver.findElement(passwordLocator).sendKeys(password);
        return this;    
    }
   
    public SuccessLoginPage submitLogin() {
       
        driver.findElement(loginButtonLocator).submit();
        return new SuccessLoginPage(driver);    
    }
   
    public FalseLoginPage submitFalseLogin() {
       
        driver.findElement(loginButtonLocator).submit();
        return new FalseLoginPage(driver);  
    }

    public SuccessLoginPage loginAs(String username, String password) {
        typeUsername(username);
        typePassword(password);
        return submitLogin();
    }
}

